import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { APIBase, ListOption } from 'app/modules/base/api-base';

@Injectable({
  providedIn: 'root',
})
export class StatisticService {
  constructor(private http: HttpClient) {}
  async systemUpgrade(...zid: number[]) {
    interface Result {
      total: number;
      need_upgrade: number;
      upgraded: number;
    }
    const result = await this.http
      .get<Result>('/api/admin/statistic/os/upgrade', { params: { area_ids: zid.join(',') } })
      .toPromise();
    return [
      { name: '最新', value: result.upgraded },
      { name: '待更新', value: result.need_upgrade },
      { name: '未知', value: result.total - result.upgraded - result.need_upgrade },
    ];
  }
  async softwareUsage(opt: {
    period: 'day' | 'week' | 'month';
    soft_ids: number[];
    area_ids: number[];
  }) {
    return this.http
      .get<SoftwareUsage>('/api/admin/statistic/software/usage', {
        params: {
          period: 'month',
          area_ids: opt.area_ids.join(','),
          soft_ids: opt.soft_ids.join(','),
        },
      })
      .toPromise();
  }

  areaSoftware() {
    interface AreaSoftwareOption extends ListOption {
      ids: number[];
    }
    const api = new APIBase<any, AreaSoftwareOption>(this.http, '/api/admin/multi_area/software');
    return api;
  }
}
interface SoftwareUsage {
  total: number;
  period: string;
  software_num: number;
  usage_num: Usagenum[];
}

interface Usagenum {
  id: number;
  name: string;
  number: number;
}
